package com.ztc.ZtcRouteAttachment.web;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.Attachment.service.AttachmentManager;
import com.aqgj.util.JsonWrapper;
import com.ztc.ZtcRouteAttachment.bean.ZtcRouteAttachment;
import com.ztc.ZtcRouteAttachment.service.ZtcRouteAttachmentManager;


@Controller
@RequestMapping("ztc_route_attachment_action")
public class ZtcRouteAttachmentAction {
	@Autowired
	private ZtcRouteAttachmentManager ztcRouteAttachmentManager;
	@Autowired
	private AttachmentManager attachmentManager;
	private static final Logger log = LogManager.getLogger(ZtcRouteAttachment.class);

	/**
	 * 添加线路附件
	 * @param ztcRouteAttachment 线路附件对象
	 * @param file 附件文件
	 * @param request
	 * @return 操作结果信息
	 */
	@RequestMapping("sign")
	@ResponseBody
	public HashMap<String, Object> save(ZtcRouteAttachment ztcRouteAttachment,
			@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		log.info("开始添加");
		HashMap<String, Object> wapper = new HashMap<>();
		String id = ztcRouteAttachmentManager.save(ztcRouteAttachment);
		if (id == null) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "重复添加");
			log.info("重复添加");
			return wapper;
		} else {
			if (file != null) {
				try {
					Attachment attachment = new Attachment();
					attachment.setFileType(file.getContentType());
					attachment.setFileSize(String.valueOf(file.getSize()));
					attachment.setFileName(file.getOriginalFilename());
					attachment.setUploadTime(new Date());
					attachment.setUploaderName("添加风景图片");
					attachment.setBusinessId(id);
					attachment.setFileBody(file.getBytes());
					String attachmentId = attachmentManager.saveAttachment(attachment, request, true);
					if (attachmentId != null) {
						attachment.setFilePath(attachmentId);
						attachmentManager.save(attachment);
						ztcRouteAttachment.setId(id);
						ztcRouteAttachment.setFkAttachmentId(attachmentId);
						String updateId = ztcRouteAttachmentManager.update(ztcRouteAttachment);
						if ("0".equals(updateId)) {
							wapper.put("success", Boolean.valueOf(false));
							wapper.put("data", "添加失败");
							log.info("添加失败");
							return wapper;
						} 
						wapper.put("attachmentId", attachmentId);
					} else {
						wapper.put("success", Boolean.valueOf(false));
						wapper.put("data", "添加失败");
						log.info("添加失败");
						return wapper;
					}
				} catch (IOException e) {
					return JsonWrapper.failureWrapperMsg(e.getMessage().toString());
				}
			}
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("data", "添加成功");
			log.info("添加成功");
			return wapper;
		}
	}

	@RequestMapping("init")
	@ResponseBody
	public HashMap<String, Object> getAll() {
		List<ZtcRouteAttachment> lists = ztcRouteAttachmentManager.getAll();
		HashMap<String, Object> wapper = new HashMap<>();
		wapper.put("success", Boolean.valueOf(true));
		wapper.put("data", lists);
		return wapper;
	}

	@RequestMapping("delete")
	@ResponseBody
	public HashMap<String, Object> delete(String id) {
		HashMap<String, Object> wapper = new HashMap<>();
		List<Attachment> list = attachmentManager.getAttachmentByBusinessId(id);
		int size = list.size();
		int count = 0;
		for (Attachment attachment : list) {
			attachmentManager.deleteAttachment(attachment);
			count++;
		}
		if (count != size) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "修改失败");
			log.info("修改失败");
		}
		String deleteResult = ztcRouteAttachmentManager.delete(id, null);
		if ("1".equals(deleteResult)) {
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("data", "删除成功");
			log.info("删除成功");
		} else {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "修改失败");
			log.info("修改失败");
		}
		return wapper;
	}

	/**
	 * 修改线路附件
	 * @param ztcClassesAttachment 线路附件
	 * @param file 附件文件
	 * @param request
	 * @return 操作结果信息
	 */
	@RequestMapping("update")
	@ResponseBody
	public HashMap<String, Object> update(ZtcRouteAttachment ztcClassesAttachment,
			@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		log.info("开始修改");
		HashMap<String, Object> wapper = new HashMap<>();
		List<Attachment> list = attachmentManager.getAttachmentByBusinessId(ztcClassesAttachment.getId());
		int size = list.size();
		int count = 0;
		for (Attachment attachment : list) {
			attachmentManager.deleteAttachment(attachment);
			count++;
		}
		if (count != size) {
			wapper.put("success", Boolean.valueOf(false));
			wapper.put("data", "修改失败");
			log.info("修改失败");
		} else {
			if (file != null) {
				try {
					Attachment attachment = new Attachment();
					attachment.setFileType(file.getContentType());
					attachment.setFileSize(String.valueOf(file.getSize()));
					attachment.setFileName(file.getOriginalFilename());
					attachment.setUploadTime(new Date());
					attachment.setUploaderName("修改风景图片");
					attachment.setBusinessId(ztcClassesAttachment.getId());
					attachment.setFileBody(file.getBytes());
					String attachmentId = attachmentManager.saveAttachment(attachment, request, true);
					if (attachmentId != null) {
						attachment.setFilePath(attachmentId);
						attachmentManager.save(attachment);
						ztcClassesAttachment.setFkAttachmentId(attachmentId);
						String updateId = ztcRouteAttachmentManager.update(ztcClassesAttachment);
						if ("0".equals(updateId)) {
							wapper.put("success", Boolean.valueOf(false));
							wapper.put("data", "添加失败");
							log.info("添加失败");
							return wapper;
						} 
					} else {
						wapper.put("success", Boolean.valueOf(false));
						wapper.put("data", "添加失败");
						log.info("添加失败");
						return wapper;
					}
				} catch (IOException e) {
					return JsonWrapper.failureWrapperMsg(e.getMessage().toString());
				}
			}
			wapper.put("success", Boolean.valueOf(true));
			wapper.put("data", "修改成功");
			log.info("修改成功");
		}
		return wapper;
	}
}